using System;
using System.Collections;

namespace ArrayListExample // 命名空间
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // 创建一个 ArrayList
            ArrayList arrayList = new ArrayList();

            // 添加元素到 ArrayList
            arrayList.Add("Alice");
            arrayList.Add("Bob");
            arrayList.Add(42);
            arrayList.Add(3.14);
            arrayList.Add(true);

            // 打印 ArrayList 的元素
            Console.WriteLine("ArrayList elements:");
            foreach (var item in arrayList)
            {
                Console.WriteLine(item);
            }

            // 替换数组中的元素
            arrayList[1] = "Charlie"; // 将 "Bob" 替换为 "Charlie"

            Console.WriteLine("\nAfter replacing an element:");
            foreach (var item in arrayList)
            {
                Console.WriteLine(item);
            }

            // 删除元素
            arrayList.Remove(42); // 删除数值 42

            Console.WriteLine("\nAfter removing an element:");
            foreach (var item in arrayList)
            {
                Console.WriteLine(item);
            }

            // 获取元素的数量
            Console.WriteLine($"\nTotal elements in ArrayList: {arrayList.Count}");

            // 清空 ArrayList
            arrayList.Clear();
            Console.WriteLine($"Total elements after clearing: {arrayList.Count}"); // 输出: 0
        }
    }
}
